package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kabuda
 * @description 针对表【edu_chapter(课程)】的数据库操作Service实现
 * @createDate 2023-04-24 14:04:24
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter>
        implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterVideo(String courseId) {
        LambdaQueryWrapper<EduChapter> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(EduChapter::getCourseId, courseId);
        List<EduChapter> list = this.list(wrapper1);


        List<EduVideo> eduVideos= eduVideoService.list(null);

        List<ChapterVo> chapterVos = new ArrayList<>();
        for (EduChapter eduChapter : list) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);

            List<VideoVo> videoVos=new ArrayList<>();
            for (EduVideo video : eduVideos) {
                if(chapterVo.getId().equals(video.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
            chapterVos.add(chapterVo);
        }

        return chapterVos;
    }
}




